package com.qire.manhua.activity;

import android.os.Bundle;

import com.qire.antsbinder.dal.exception.DataAccessException;
import com.qire.antscore.annotation.DalExceptionObserve;
import com.qire.antscore.annotation.RouteMapping;
import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.inject.Autowired;
import com.qire.antsrouter.lifecycleHandler.ActivityManageHandler;
import com.qire.common.constant.CustomEvent;
import com.qire.common.constant.Navigation;
import com.qire.common.constant.TeensModeManage;
import com.qire.common.constant.Url;
import com.qire.common.inject.EventControllerInjector;
import com.qire.common.inject.ViewModelInjector;
import com.qire.common.support.base.BaseActivity;
import com.qire.common.utils.NoticeEventBus;
import com.qire.common.utils.NoticeEventBus.EventSubscriber;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.R;
import com.qire.manhua.controller.TeensLockTimeoutController;
import com.qire.manhua.databinding.ActivityTeensLockTimeoutBinding;
import com.qire.manhua.model.viewModel.impl.TeensLockTimeoutViewModel;

import androidx.annotation.Nullable;

/**
 * 青少年模式：限时阅读超时锁定页（40分钟超时）
 */
@RouteMapping(path = Navigation.Page.TeensLockTimeoutPage)
public class TeensLockTimeoutActivity extends BaseActivity<ActivityTeensLockTimeoutBinding> {

    @Autowired(injector = ViewModelInjector.class)
    TeensLockTimeoutViewModel viewModel;

    @Autowired(injector = EventControllerInjector.class)
    TeensLockTimeoutController controller;

    @Override
    public int layoutRid() {
        return R.layout.activity_teens_lock_timeout;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        viewModel.onAddition();
        controller.onAddition(viewModel);

    }

    @Override
    public void finish() {
        if(!TeensModeManage.teensModeEnable()) {
            // 如果青少年模式开启状态中，则不可以关闭当前页面
            super.finish();
        }
    }

    @Override
    protected void onDestroy() {
        if(ActivityManageHandler.HANDLER.isLastOne()) {
            AntsRouter.ROUTER.buildCard(Navigation.Page.HomePage).navigation();
        }
        super.onDestroy();
    }

    @Override
    protected void registerObserver() {
        super.registerObserver();
        NoticeEventBus.NOTICE.subscribeEvent(CustomEvent.TEENS_MODE_VERIFY, verifyEventSubscriber);
    }

    @Override
    protected void unregisterObserver() {
        super.unregisterObserver();
        NoticeEventBus.NOTICE.unSubscribeEvent(verifyEventSubscriber);
    }

    @DalExceptionObserve(tags = Url.TeensModeVerifyPassword)
    public void verifyPasswordExceptionHandle(DataAccessException e) {
        ToastHelper.showSnackBar("密码错误，验证失败", Prompt.ERROR);
    }

    private EventSubscriber<Boolean> verifyEventSubscriber = event -> {
        TeensModeManage.authorizeContinue();
        super.finish();
        return true;
    };
}
